package com.anywhere.freightman.order.service.impl;

import com.anywhere.freightman.common.BaseEntityService;
import com.anywhere.freightman.common.BaseMapper;
import com.anywhere.freightman.common.entity.order.CompanyQueryEntity;
import com.anywhere.freightman.common.enumeration.CommonStatus;
import com.anywhere.freightman.common.enumeration.RoleType;
import com.anywhere.freightman.common.utils.BPwdEncoderUtils;
import com.anywhere.freightman.order.controller.request.CompanyRequest;
import com.anywhere.freightman.order.dao.CompanyDao;
import com.anywhere.freightman.order.model.CompanyModel;
import com.anywhere.freightman.order.service.ICompanyService;
import com.anywhere.freightman.system.controller.request.CompanyRegRequest;
import com.anywhere.freightman.system.model.UserModel;
import com.anywhere.freightman.system.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

@Service
@Transactional
public class CompanyService extends BaseEntityService<CompanyModel, CompanyQueryEntity> implements ICompanyService {

    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private IUserService userService;
    @Value("${system.default-pwd}")
    private String defaultPwd;

    @Override
    public BaseMapper<CompanyModel> getBaseDao() {
        return companyDao;
    }

    @Override
    public Example getExample(CompanyQueryEntity entity) {
        Weekend<CompanyModel> weekend = Weekend.of(CompanyModel.class);
        WeekendCriteria<CompanyModel, Object> criteria = weekend.weekendCriteria();

        if (entity == null) {
            return weekend;
        }

        if (entity.getUnitName() != null) {
            criteria.andEqualTo("unitName", entity.getUnitName());
        }

        return weekend;
    }

    @Override
    public void insert(CompanyModel model) {
        model.setStatus(CommonStatus.ENABLE);
        super.insert(model);
    }

    @Override
    public void insert(CompanyRequest entity) {
        CompanyModel model = new CompanyModel();
        BeanUtils.copyProperties(entity, model);
        super.insert(model);
        entity.setId(model.getId());

        UserModel userModel = new UserModel();
        userModel.setNickname(model.getContact());
        userModel.setUsername(entity.getUsername());
        userModel.setMobilePhone(entity.getTel());
        userModel.setCompanyId(model.getId());
        userModel.setType(RoleType.TENANT);
        userModel.setPassword(defaultPwd);
        userService.insert(userModel);

    }

    @Override
    public void infoReg(CompanyRegRequest request) {
        CompanyModel model = new CompanyModel();
        BeanUtils.copyProperties(request, model);
        super.insert(model);
        request.setId(model.getId());

        UserModel userModel = new UserModel();
        userModel.setNickname(model.getContact());
        userModel.setUsername(request.getUsername());
        userModel.setMobilePhone(request.getTel());
        userModel.setCompanyId(model.getId());
        userModel.setType(RoleType.TENANT);
        userModel.setPassword(request.getPassword());
        userService.insert(userModel, CommonStatus.DISABLE);
    }

    @Override
    public void delete(CompanyModel model) {

        List<UserModel> userList = userService.getListByCompanyId(model.getId());
        userList.forEach(item -> userService.delete(item));

        super.delete(model);
    }

    public static void main(String[] args) {
//        String pwd = BPwdEncoderUtils.BCryptPassword("000000");
        String pwd = "{bcrypt}$2a$10$UKpcPkPUeYRe9cChshoTBeMBpcCxUAjIOW21brvy65CPKrQ.YPc.y";

//        String pwd = passwordEncoder.encode("000000");
        System.out.println(pwd);

        boolean matches = BPwdEncoderUtils.matches("000000", pwd);
        System.out.println(matches);
    }
}
